package o2o.business.device;

import com.jinyou.utils.common.ValidateUtil;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import o2o.cache.postman.CityPostmanMemory;
import o2o.cache.shop.ShopExtraInfoMemory;
import o2o.cache.user.UserInfoExtraMemory;
import o2o.config.dao.DataDaoSql;
import o2o.config.dao.UserDaoSql;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.common.UserType;
import o2o.dao.PO.order.OrderDeviceInfo;
import o2o.postman.model.CityPostman;
import o2o.dao.PO.shop.ShopInfoExtra;
import o2o.dao.PO.user.UserInfoExtra;
import org.apache.commons.lang.StringUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashSet;

// 设备信息处理
public class AppDeviceInfoBusiness {
    private static HashSet<AppDeviceInfo> deviceSet = new HashSet<>();

    private static HashSet<OrderDeviceInfo> orderDeviceSet = new HashSet<>();

    public static void shopAddDevice(Long shopId, String devJson) {
        if (ValidateUtil.isNotID(shopId) || ValidateUtil.isNull(devJson)) {
            return;
        }
        AppDeviceInfo appDeviceInfo = new AppDeviceInfo();
        appDeviceInfo.setShopId(shopId);
        appDeviceInfo.setUserType(UserType.SHOP_OWNER);
        appDeviceInfo.setDevJson(devJson);
        deviceSet.add(appDeviceInfo);
    }

    public static void postmanAddDevice(String username, String devJson) {
        if (ValidateUtil.isNull(username) || ValidateUtil.isNull(devJson)) {
            return;
        }
        AppDeviceInfo appDeviceInfo = new AppDeviceInfo();
        appDeviceInfo.setUsername(username);
        appDeviceInfo.setUserType(UserType.POSTMAN);
        appDeviceInfo.setDevJson(devJson);
        deviceSet.add(appDeviceInfo);
    }

    public static void userAddDevice(String username, String devJson) {
        if (ValidateUtil.isNull(username) || ValidateUtil.isNull(devJson)) {
            return;
        }
        AppDeviceInfo appDeviceInfo = new AppDeviceInfo();
        appDeviceInfo.setUsername(username);
        appDeviceInfo.setUserType(UserType.USER);
        appDeviceInfo.setDevJson(devJson);
        deviceSet.add(appDeviceInfo);
    }

    //-------------------------------------

    // 处理用户设备信息
    public static void doAppDeviceInfo() {
        if (deviceSet == null || deviceSet.size() < 1) {
            return;
        }
        HashSet<AppDeviceInfo> needSet = new HashSet<>();
        synchronized (deviceSet) {
            needSet.addAll(deviceSet);
            deviceSet.clear();
        }
        if (null == needSet || needSet.size() < 1) {
            return;
        }
        for (AppDeviceInfo deviceInfo : needSet) {
            if (null == deviceInfo) {
                continue;
            }
            boolean dispatchSuccess = doInfo(deviceInfo);
            // if (dispatchSuccess) {
            //     deviceSet.remove(deviceInfo);
            // }
            if (!dispatchSuccess) {
                deviceSet.add(deviceInfo);
            }
        }
    }

    private static boolean doInfo(AppDeviceInfo deviceInfo) {
        if (null == deviceInfo) {
            return true;
        }
        if (null != deviceInfo.getUserType()) {
            switch (deviceInfo.getUserType()) {
                case UserType.USER: {// 用户
                    String userName = deviceInfo.getUsername();
                    if (StringUtils.isBlank(userName)) {
                        return true;
                    }
                    UserInfoExtra userInfoExtra = UserInfoExtraMemory.getInstance().getByUsername(deviceInfo.getUsername());
                    if (null == userInfoExtra) {
                        return true;
                    }
                    if (ValidateUtil.isNotNull(userInfoExtra.getDevInfo()) && userInfoExtra.getDevInfo().equalsIgnoreCase(deviceInfo.getDevJson())) {
                        return true;
                    }
                    // 设备信息改变了

                    // 存入数据库
                    StringBuffer sb = new StringBuffer();
                    sb.append(" update ").append(PojoTables.USER_INFO_EXTRA).append(" set devInfo = '").append(deviceInfo.getDevJson()).append("' where delFlag = 0 and username = '").append(userName).append("'");
                    try {
                        UserDaoSql.getInstance().executeUpdateBySql(sb.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return true;
                    }
                    UserInfoExtraMemory.getInstance().addOrModify(userName);
                    return true;
                }
                case UserType.SHOP_OWNER: {// 商家
                    Long shopId = deviceInfo.getShopId();
                    if (ValidateUtil.isNotID(shopId)) {
                        return true;
                    }
                    ShopInfoExtra extra = ShopExtraInfoMemory.getInstance().getShopExtraInfoById(shopId);
                    if (null == extra) {
                        return true;
                    }
                    if (ValidateUtil.isNotNull(extra.getDevInfo()) && extra.getDevInfo().equalsIgnoreCase(deviceInfo.getDevJson())) {
                        return true;
                    }
                    // 设备信息改变了

                    // 存入数据库
                    StringBuffer sb = new StringBuffer();
                    sb.append(" update ").append(PojoTables.SHOP_INFO_EXTRA).append(" set devInfo='").append(deviceInfo.getDevJson()).append("' where delFlag = 0 and shopId = ").append(shopId);
                    try {
                        UserDaoSql.getInstance().executeUpdateBySql(sb.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return true;
                    }

                    ShopExtraInfoMemory.getInstance().addOrModify(shopId);
                    return true;
                }
                case UserType.POSTMAN: {// 骑手
                    String username = deviceInfo.getUsername();
                    if (ValidateUtil.isNull(username)) {
                        return true;
                    }
                    CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(username);
                    if (null == cityPostman) {
                        return true;
                    }
                    if (ValidateUtil.isNotNull(cityPostman.getDevInfo()) && cityPostman.getDevInfo().equalsIgnoreCase(deviceInfo.getDevJson())) {
                        return true;
                    }
                    // 设备信息改变了

                    // 存入数据库
                    StringBuffer sb = new StringBuffer();
                    sb.append(" update ").append(PojoTables.CityPostman).append(" set devInfo='").append(deviceInfo.getDevJson()).append("' where delFlag = 0 and username = '").append(username).append("'");
                    try {
                        UserDaoSql.getInstance().executeUpdateBySql(sb.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return true;
                    }
                    CityPostmanMemory.getInstance().modifyByUsername(username);
                    try {
                        JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POSTMAN_INFO_MODIFY).setStr01(username);
                        JmsSender.setTopicSender(notify);
                    } catch (Exception e) {
                    }
                    return true;
                }
            }
        }

        return false;
    }

    public static void orderAddDevice(String orderNo, String devJson, String username) {
        if (ValidateUtil.isNull(orderNo) || ValidateUtil.isNull(devJson) || ValidateUtil.isNull(username)) {
            return;
        }
        OrderDeviceInfo orderDeviceInfo = new OrderDeviceInfo();
        orderDeviceInfo.setOrderNo(orderNo);
        orderDeviceInfo.setDevInfo(devJson);
        orderDeviceInfo.setCreateUser(username);
        orderDeviceInfo.setCreateTime(new Date().getTime());
        orderDeviceSet.add(orderDeviceInfo);
    }

    public static void doOrderDeviceInfo() {
        if (orderDeviceSet == null || orderDeviceSet.size() < 1) {
            return;
        }
        HashSet<OrderDeviceInfo> needSet = new HashSet<>();
        synchronized (orderDeviceSet) {
            needSet.addAll(orderDeviceSet);
            orderDeviceSet.clear();
        }
        if (null == needSet || needSet.size() < 1) {
            return;
        }
        for (OrderDeviceInfo orderDeviceInfo : needSet) {
            if (null == orderDeviceInfo) {
                continue;
            }
            try {
                DataDaoSql.getInstance().saveObj(PojoTables.D_ORDER_DEVICE_INFO, orderDeviceInfo);
            } catch (SQLException e) {
                e.printStackTrace();
                orderDeviceSet.add(orderDeviceInfo);
            }
        }
    }
}
